package sistema.datos;

import java.sql.*;
import javax.sql.*;
import javax.servlet.*;

import sistema.*;

public class Base {
    private Connection connection;
    private static String database_driver;
    private static String url_conexion;
    private static String username;
    private static String password;
    
    public static void setDataSourceConexion(String dd, String uc, String u, String p) {
    	    System.out.println("DataConexion");
    	    database_driver = dd;
    	    url_conexion = uc;
    	    username = u;
    	    password = p;
    }
        
    public Connection getConexion() throws MBE_Exception {
        if (connection!=null)
            return connection;
        
        try {
            Class.forName(database_driver).newInstance();
            connection = DriverManager.getConnection(url_conexion, username, password);
            return connection;
        }
        catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage()); 
            System.out.println("SQLState: " + e.getSQLState()); 
            System.out.println("VendorError: " + e.getErrorCode()); 
            throw new MBE_Exception(e);
        }
        catch (Exception e) {
            throw new MBE_Exception(e);
        }
    }
        
    public void dispose() throws MBE_Exception {
        if (connection==null)
            return;
        
        try {
            connection.close();
            connection=null;
        }
        catch (SQLException e) {
            System.out.println("SQLException: " + e.getMessage()); 
            System.out.println("SQLState: " + e.getSQLState()); 
            System.out.println("VendorError: " + e.getErrorCode()); 
            throw new MBE_Exception(e);
        }
    }
}

